<template>
  <div class="main">
    <!-- 发送按钮 -->
    <div class="button-container">
      <el-button type="primary" @click="open">发送</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>
    <!-- 对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="消息内容">
          <el-input v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="commit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 表格 -->
    <div class="table-container">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="addTime" label="日期" width="180">
        </el-table-column>
        <el-table-column prop="content" label="内容" width="180">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        content: "",
        toUserId: 1,
        isAdmin: 0,
      },
      tableData: [],
    };
  },
  mounted() {
    this.getMessage();
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    open() {
      this.dialogVisible = true;
    },
    commit() {
      // 使用 axios 发送 POST 请求
      this.$http
        .post("yonghu/send", this.form)
        .then((res) => {
          // 清空表单中的内容
          this.form.content = "";
          // 关闭对话框
          this.dialogVisible = false;
          // 重新获取消息列表
          this.getMessage();
        })
        .catch((error) => {
          console.error("请求出错:", error);
        });
    },
    getMessage() {
      this.$http
        .post("yonghu/list", {})
        .then((res) => {
          this.tableData = res.data.data;
        })
        .catch((error) => {
          console.error("请求出错:", error);
        });
    },
    refresh(){
      this.getMessage();
    }
  },
};
</script>

<style scoped>
.main {
  padding: 50px;
  box-sizing: border-box;
}

.button-container {
  text-align: center;
  margin-bottom: 20px;
}

.table-container {
  display: flex;
  justify-content: center;
  align-items: flex-start; /* 表格顶部对齐 */
  min-height: auto; /* 取消占满视口高度 */
  padding: 20px;
}

.el-table {
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}
</style>

